package org.ims.inv.model.project;

import java.util.HashSet;
import java.util.Set;

import cn.trymore.core.model.ModelBase;
import cn.trymore.core.util.UtilCollection;

/**
 * Model: 项目材料
 * 
 * @author Jeccy.Zhao
 *
 */
public class ModelProjectMaterial
extends ModelBase
{

	/**
	 * Serial UID
	 */
	private static final long serialVersionUID = -7687809128704999005L;
	
	/**
	 * 材料名称
	 */
	private String materialName;
	
	/**
	 * 材料描述
	 */
	private String materialDesc;
	
	/**
	 * 材料性质
	 */
	private Integer materialType;
	
	/**
	 * 父材料
	 */
	private ModelProjectMaterial materialParent;
	
	/**
	 * 子材料
	 */
	private Set<ModelProjectMaterial> materialChildren;
	
	/**
	 * 所属项目
	 */
	private String projectNo;
	
	/**
	 * 材料元信息
	 */
	private Set<ModelProjectMaterialMeta> metas;
	
	/**
	 * 序号
	 */
	private Integer sequence;
	
	/**
	 * Adds entity to children set
	 * @param entity
	 */
	public void addSubMaterial (ModelProjectMaterial entity)
	{
		if (materialChildren == null)
		{
			materialChildren = new HashSet<ModelProjectMaterial>();
		}
		
		boolean existed = false;
		
		if (entity.getId() != null)
		{
			for (ModelProjectMaterial sub : materialChildren)
			{
				if (sub.getId().equals(entity.getId()))
				{
					existed = true;
					break;
				}
			}
		}
		
		if (!existed)
		{
			materialChildren.add(entity);
		}
	}
	
	/**
	 * Adds entity to meta set
	 * @param entity
	 */
	public void addMeta (ModelProjectMaterialMeta entity)
	{
		if (metas == null)
		{
			metas = new HashSet<ModelProjectMaterialMeta>();
		}
		
		boolean existed = false;
		
		if (entity.getId() != null)
		{
			for (ModelProjectMaterialMeta sub : metas)
			{
				if (sub.getId().equals(entity.getId()))
				{
					existed = true;
					break;
				}
			}
		}
		
		if (!existed)
		{
			metas.add(entity);
		}
	}
	
	/**
	 * 获取最新提交时间
	 * 
	 * @return
	 */
	public ModelProjectMaterialMeta getLatestUploadMeta ()
	{
		if (UtilCollection.isNotEmpty(this.getMetas()))
		{
			for (ModelProjectMaterialMeta meta : this.getMetas())
			{
				return meta;
			}
		}
		
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	public int getTotalMaterialCount ()
	{
		return this.getSingleMaterialCount(this);
	}
	
	/**
	 * 
	 * @param entity
	 * @return
	 */
	private int getSingleMaterialCount (ModelProjectMaterial entity)
	{
		int count = UtilCollection.isNotEmpty(entity.getMetas()) ? 1 : 0;
		
		if (UtilCollection.isNotEmpty(entity.getMaterialChildren()))
		{
			for (ModelProjectMaterial child : entity.getMaterialChildren())
			{
				count += getSingleMaterialCount(child);
			}
		}
		
		return count;
	}
	
	public void setMetas(Set<ModelProjectMaterialMeta> metas)
	{
		this.metas = metas;
	}

	public Set<ModelProjectMaterialMeta> getMetas()
	{
		return metas;
	}
	
	public void setMaterialName(String materialName)
	{
		this.materialName = materialName;
	}

	public String getMaterialName()
	{
		return materialName;
	}

	public void setMaterialDesc(String materialDesc)
	{
		this.materialDesc = materialDesc;
	}

	public String getMaterialDesc()
	{
		return materialDesc;
	}

	public void setMaterialType(Integer materialType)
	{
		this.materialType = materialType;
	}

	public Integer getMaterialType()
	{
		return materialType;
	}

	public void setProjectNo(String projectNo)
	{
		this.projectNo = projectNo;
	}

	public String getProjectNo()
	{
		return projectNo;
	}

	public void setMaterialParent(ModelProjectMaterial materialParent)
	{
		this.materialParent = materialParent;
	}

	public ModelProjectMaterial getMaterialParent()
	{
		return materialParent;
	}
	
	public Set<ModelProjectMaterial> getMaterialChildren()
	{
		return materialChildren;
	}

	public void setMaterialChildren(Set<ModelProjectMaterial> materialChildren)
	{
		this.materialChildren = materialChildren;
	}
	
	public Integer getSequence()
	{
		return sequence;
	}

	public void setSequence(Integer sequence)
	{
		this.sequence = sequence;
	}
}
